package com.p2p.front.web.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.p2p.base.model.BaseUser;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.dal.dao.UserDao;
import com.p2p.front.service.UserInfoAuthService;
import com.p2p.front.service.UserInfoService;
import com.p2p.front.vo.UserInfoVO;
import com.p2p.user.enums.UserInfoName;

/**
 * @author ruiqi
 *
 */
@Controller
@RequestMapping("/info/*")
public class UserInfoController extends BaseController{

	@Resource
	private UserInfoAuthService userInfoAuthService;
	
	@Resource
	private UserInfoService userInfoService;
	
	@Resource
	private UserDao userDao;
	
	List<String> personalInfoNameList;
	
	List<String> jobInfoNameList;
	
	List<String> contactInfoNameList;
	
	public UserInfoController() {
		init();
	}
	
	private void init(){
		personalInfoNameList = new ArrayList<String>();
		
		personalInfoNameList.add(UserInfoName.GENDER.name());
		personalInfoNameList.add(UserInfoName.BIRTHDATE.name());//出生日期
		personalInfoNameList.add(UserInfoName.IDCARD_PROVINCE.name());//户籍省份
		personalInfoNameList.add(UserInfoName.IDCARD_CITY.name());//户籍城市
		personalInfoNameList.add(UserInfoName.PROVINCE.name());//省份
		personalInfoNameList.add(UserInfoName.CITY.name());//城市
		personalInfoNameList.add(UserInfoName.DISTRICT.name());//区域
		personalInfoNameList.add(UserInfoName.ADDRESS.name());//详细居住地址
		personalInfoNameList.add(UserInfoName.ADDRESS_PHONE.name());//居住地电话
		personalInfoNameList.add(UserInfoName.EDUCATION.name());//学历:0-未知.name());1-高中或以下.name());2-大专.name());3-本科.name());4-研究生或以上
		personalInfoNameList.add(UserInfoName.GRADUATED_YEAR.name());//毕业年份
		personalInfoNameList.add(UserInfoName.GRADUATED_SCHOOL.name());//毕业学校
		personalInfoNameList.add(UserInfoName.MARRIAGE.name());//婚姻状况:0-未知.name());1-已婚.name());2-离异.name());3-未婚
		personalInfoNameList.add(UserInfoName.HAVE_CHILD.name());//有无子女:0-未知.name());1-有.name());2-无
		personalInfoNameList.add(UserInfoName.HAVE_HOUSE.name());//住房情况:0-未知.name());1-无.name());2-商品房.name());3-自建房
		personalInfoNameList.add(UserInfoName.HOUSE_LOAN.name());//有无房贷:0-未知.name());1-有.name());2-无
		personalInfoNameList.add(UserInfoName.HAVE_CAR.name());//是否有车:0-未知.name());1-有.name());2-无
		personalInfoNameList.add(UserInfoName.CAR_BRAND.name());//汽车品牌
		personalInfoNameList.add(UserInfoName.CAR_YEAR.name());//购车年份	
		personalInfoNameList.add(UserInfoName.CAR_LOAN.name());//有无车贷:0-未知.name());1-有.name());2-无
		
		
		
		jobInfoNameList = new ArrayList<String>();
		
		jobInfoNameList.add(UserInfoName.JOB_COMPANY.name());//单位名称或个人
		jobInfoNameList.add(UserInfoName.JOB_TYPE.name());//工作类型: 0-未知, 1-工薪阶层,2-私营企业主,3-自由职业,4-网络商家,5-其他
		jobInfoNameList.add(UserInfoName.JOB_COMPANY_TYPE.name());//公司类别
		jobInfoNameList.add(UserInfoName.JOB_COMPANY_SCALE.name());//公司规模
		jobInfoNameList.add(UserInfoName.JOB_INDUSTRY.name());//行业
		jobInfoNameList.add(UserInfoName.JOB_TITLE.name());//职位
		jobInfoNameList.add(UserInfoName.JOB_SALARY.name());//月收入
		jobInfoNameList.add(UserInfoName.JOB_YEARS.name());//工作年限
		jobInfoNameList.add(UserInfoName.JOB_EMAIL.name());//工作邮箱
		jobInfoNameList.add(UserInfoName.JOB_PROVINCE.name());//工作省份
		jobInfoNameList.add(UserInfoName.JOB_CITY.name());//工作城市
		jobInfoNameList.add(UserInfoName.JOB_COMPANY_ADDRESS.name());//公司地址
		jobInfoNameList.add(UserInfoName.JOB_COMPANY_PHONE.name());//公司电话
		
		contactInfoNameList = new ArrayList<String>();
		contactInfoNameList.add(UserInfoName.FAMILY_NAME.name());//亲属姓名
		contactInfoNameList.add(UserInfoName.FAMILY_WHO.name());//关系
		contactInfoNameList.add(UserInfoName.FAMILY_PHONE.name());//亲属联系人手机或电话
		contactInfoNameList.add(UserInfoName.OTHER_NAME.name());//其它联系人姓名
		contactInfoNameList.add(UserInfoName.OTHER_WHO.name());//关系
		contactInfoNameList.add(UserInfoName.OTHER_PHONE.name());//其它联系人手机或电话
		
		
	}
	@RequestMapping("/info/viewInfo")
	public String viewInfo(ModelMap model){
		BaseUser user = getCurrentUser();
		UserInfoVO userInfoVO = userInfoService.findByNameEnums(user.getId(),Arrays.asList(UserInfoName.values()));
		Map<String, Object> userCertInfo= userDao.get(Long.parseLong(user.getId()));
		model.addAttribute("infoMap", userInfoVO.getInfoMap());
		model.addAttribute("realname", userCertInfo.get("realname"));
		model.addAttribute("personalInfoNameList", personalInfoNameList);
		model.addAttribute("jobInfoNameList", jobInfoNameList);
		model.addAttribute("contactInfoNameList", contactInfoNameList);
		model.addAttribute("contactInfoName", userInfoVO.getInfoMap().get(UserInfoName.FAMILY_NAME.name()));
		model.addAttribute("contactInfoPhone", userInfoVO.getInfoMap().get(UserInfoName.FAMILY_PHONE.name()));
		model.addAttribute("relationShip", userInfoVO.getInfoMap().get(UserInfoName.FAMILY_WHO.name()));
		
		return "userInfo/viewInfo";
	}
	
	@RequestMapping("/info/saveInfo")
	public String saveInfo(UserInfoVO userInfoVO){
		
		BaseUser user = getCurrentUser();
		
		userInfoVO.setUserId(user.getId());
		
		userInfoService.batchSave(userInfoVO);
		
		return "redirect:/info/viewInfo.html";
	}	
}
